/*
 * AddMountDialog.java
 *
 * Created on January 29, 2004, 12:38 PM
 */
package ps2vfs.gui;


/**
 *
 * @author  stigpo
 */
public class AddMountDialog extends javax.swing.JDialog {
    private ps2vfs.vfs.Ps2Vfs vfs;

    /** Creates new form AddMountDialog */
    public AddMountDialog(java.awt.Frame parent, boolean modal,
			  ps2vfs.vfs.Ps2Vfs vfs) {
        super(parent, modal);
	this.vfs = vfs;
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    private void initComponents() {//GEN-BEGIN:initComponents
        jPanel2 = new javax.swing.JPanel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jPanel3 = new javax.swing.JPanel();
        jPanel1 = new javax.swing.JPanel();
        attachedUrlField = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jPanel4 = new javax.swing.JPanel();
        jTextField2 = new javax.swing.JTextField();
        jPanel5 = new javax.swing.JPanel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jCheckBox2 = new javax.swing.JCheckBox();

        setTitle("Add new mount point");
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                closeDialog(evt);
            }
        });

        jPanel2.setLayout(new javax.swing.BoxLayout(jPanel2, javax.swing.BoxLayout.X_AXIS));

        jPanel2.setMaximumSize(new java.awt.Dimension(128, 25));
        jPanel2.setPreferredSize(new java.awt.Dimension(128, 40));
        jButton1.setText("OK");
        jButton1.setAlignmentX(0.5F);
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton1);

        jPanel2.add(javax.swing.Box.createRigidArea(new java.awt.Dimension(20, 0)));
        jButton2.setText("Cancel");
        jButton2.setAlignmentX(0.5F);
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jPanel2.add(jButton2);

        getContentPane().add(jPanel2, java.awt.BorderLayout.SOUTH);

        jPanel3.setLayout(new javax.swing.BoxLayout(jPanel3, javax.swing.BoxLayout.Y_AXIS));

        jPanel1.setLayout(new javax.swing.BoxLayout(jPanel1, javax.swing.BoxLayout.X_AXIS));

        jPanel1.setBorder(new javax.swing.border.TitledBorder("Attach URL"));
        jPanel1.setAlignmentX(0.0F);
        jPanel1.setMaximumSize(new java.awt.Dimension(2147483647, 50));
        jPanel1.setMinimumSize(new java.awt.Dimension(14, 50));
        jPanel1.setPreferredSize(new java.awt.Dimension(14, 50));
        jPanel1.add(attachedUrlField);

        jButton3.setText("...");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                OpenFileDialog(evt);
            }
        });

        jPanel1.add(jButton3);

        jPanel3.add(jPanel1);

        jPanel4.setLayout(new javax.swing.BoxLayout(jPanel4, javax.swing.BoxLayout.X_AXIS));

        jPanel4.setBorder(new javax.swing.border.TitledBorder("Mount at"));
        jPanel4.setAlignmentX(0.0F);
        jPanel4.setMaximumSize(new java.awt.Dimension(2147483647, 50));
        jPanel4.setMinimumSize(new java.awt.Dimension(14, 50));
        jPanel4.setPreferredSize(new java.awt.Dimension(14, 50));
        jPanel4.add(jTextField2);

        jPanel3.add(jPanel4);

        jPanel5.setLayout(new javax.swing.BoxLayout(jPanel5, javax.swing.BoxLayout.X_AXIS));

        jPanel5.setBorder(new javax.swing.border.TitledBorder("Options"));
        jPanel5.setAlignmentX(0.0F);
        jCheckBox1.setText("Append name");
        jCheckBox1.setToolTipText("Append name of last path element to \"Mount at\"");
        jPanel5.add(jCheckBox1);

        jCheckBox2.setSelected(true);
        jCheckBox2.setText("Include sub-directories");
        jCheckBox2.setToolTipText("Include sub-directories in VFS.");
        jPanel5.add(jCheckBox2);

        jPanel3.add(jPanel5);

        getContentPane().add(jPanel3, java.awt.BorderLayout.CENTER);

        pack();
    }//GEN-END:initComponents

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Add your handling code here:
        statusOK = 0;
        setVisible(false);
        dispose();
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Add your handling code here
        // System.out.println("btn1Action");
      String physPath = attachedUrlField.getText();
      String virtPath = jTextField2.getText();
      if((physPath == null || physPath.length() <= 0) &&
	 (virtPath == null || virtPath.length() <= 0)) {
	statusOK = 0;
      } else {
	/*
	System.err.println("Final: " + attachedUrlField.getText() + " at " +
			   jTextField2.getText() + " " + 
			   (jCheckBox1.isSelected() ? "e" : "-" ) + 
			   (jCheckBox2.isSelected() ? "r" : "-" )); */
	ps2vfs.vfs.VfsMountTable mntTable = vfs.getMountTable();
	mntTable.addMountPoint(virtPath, physPath, jCheckBox1.isSelected(), jCheckBox2.isSelected(), false);
	vfs.persistParams();
	statusOK = 1;
      }
      setVisible(false);
      dispose();
    }//GEN-LAST:event_jButton1ActionPerformed

    private void OpenFileDialog(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_OpenFileDialog
        // Add your handling code here:
        
        javax.swing.JFileChooser chooser = new javax.swing.JFileChooser();
        java.io.File curSel = new java.io.File(attachedUrlField.getText());
        chooser.setFileSelectionMode(javax.swing.JFileChooser.DIRECTORIES_ONLY);
        chooser.setSelectedFile(curSel);
        //chooser.setCurrentDirectory(new java.io.File("/h/stigpo"));
        int retVal = chooser.showDialog(this, "Use");
        if(retVal == javax.swing.JFileChooser.APPROVE_OPTION) {
            java.io.File sel = chooser.getSelectedFile();
            if(sel != null) {
                attachedUrlField.setText(sel.getAbsolutePath());
            }
        }
            
    }//GEN-LAST:event_OpenFileDialog
    
    /** Closes the dialog */
    private void closeDialog(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_closeDialog
        //setVisible(false);
        //dispose();
        System.out.println("Closing");
    }//GEN-LAST:event_closeDialog
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        new AddMountDialog(new javax.swing.JFrame(), true, null).show();
    }
    
    public int showDialog() {
       statusOK = 0;
       
       setVisible(true);
       
       return statusOK;
     }
    
    private int statusOK = 0;
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JTextField attachedUrlField;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JCheckBox jCheckBox2;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JPanel jPanel4;
    private javax.swing.JPanel jPanel5;
    private javax.swing.JTextField jTextField2;
    // End of variables declaration//GEN-END:variables
    
}
